package com.wing.cabinet.utils;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
@Slf4j
public class DjInterfaceAbutment {

    private static final Map<String,String> SYSTEM_CACHE = new HashMap<>();
    /**
     * appid
     */
    private static final String APPID = "OAAPPID2023";
    public static void main(String[] args) {
        String restful = testRestful("http://116.6.107.206:8085", "/api/workflow/paService/doCreateRequest", null, null, "1");
        /*System.out.println("testRestful:东江接口对接:"  + "---返回值：" + restful);*/
    }
    /**
     * 第一步：
     *
     * 调用ecology注册接口,根据appid进行注册,将返回服务端公钥和Secret信息
     */
    public static Map<String,Object> testRegist(String address){
        //获取当前系统RSA加密的公钥
        RSA rsa = new RSA();
        String publicKey = rsa.getPublicKeyBase64();
        String privateKey = rsa.getPrivateKeyBase64();
        // 客户端RSA私钥
        SYSTEM_CACHE.put("LOCAL_PRIVATE_KEY",privateKey);
        // 客户端RSA公钥
        SYSTEM_CACHE.put("LOCAL_PUBLIC_KEY",publicKey);
        //调用ECOLOGY系统接口进行注册
        String data = HttpRequest.post(address + "/api/ec/dev/auth/regist")
                .header("appid",APPID)
                .header("cpk",publicKey)
                .timeout(2000)
                .execute().body();
        // 打印ECOLOGY响应信息
        System.out.println("testRegist()："+data);
        Map<String,Object> datas = JSONUtil.parseObj(data);
        //ECOLOGY返回的系统公钥
        SYSTEM_CACHE.put("SERVER_PUBLIC_KEY",StrUtil.nullToEmpty((String)datas.get("spk")));
        //ECOLOGY返回的系统密钥
        SYSTEM_CACHE.put("SERVER_SECRET",StrUtil.nullToEmpty((String)datas.get("secrit")));
        return datas;
    }
    /**
     * 第二步：
     *
     * 通过第一步中注册系统返回信息进行获取token信息
     */
    public static Map<String,Object> testGetoken(String address){
        // 从系统缓存或者数据库中获取ECOLOGY系统公钥和Secret信息
        String secret = SYSTEM_CACHE.get("SERVER_SECRET");
        String spk = SYSTEM_CACHE.get("SERVER_PUBLIC_KEY");
        // 如果为空,说明还未进行注册,调用注册接口进行注册认证与数据更新
        if (Objects.isNull(secret)||Objects.isNull(spk)){
            testRegist(address);
            // 重新获取最新ECOLOGY系统公钥和Secret信息
            secret = SYSTEM_CACHE.get("SERVER_SECRET");
            spk = SYSTEM_CACHE.get("SERVER_PUBLIC_KEY");
        }
        // 公钥加密,所以RSA对象私钥为null
        RSA rsa = new RSA(null,spk);
        //对秘钥进行加密传输，防止篡改数据
        String encryptSecret = rsa.encryptBase64(secret, CharsetUtil.CHARSET_UTF_8, KeyType.PublicKey);
        //调用ECOLOGY系统接口进行注册
        String data = HttpRequest.post(address+ "/api/ec/dev/auth/applytoken")
                .header("appid",APPID)
                .header("secret",encryptSecret)
                .header("time","3600")
                .execute().body();
        log.info("testGetoken()："+data);
        Map<String,Object> datas = JSONUtil.parseObj(data);
        //ECOLOGY返回的token
        // TODO 为Token缓存设置过期时间
        SYSTEM_CACHE.put("SERVER_TOKEN",StrUtil.nullToEmpty((String)datas.get("token")));
        return datas;
    }
    /**
     * 第三步：
     *
     * 调用ecology系统的rest接口，请求头部带上token和用户标识认证信息
     *
     * @param address ecology系统地址
     * @param api rest api 接口地址(该测试代码仅支持GET请求)
     * @param mainDataStr 请求参数json串
     *  @param detailStr 请求参数json串
     *
     * 注意：ECOLOGY系统所有POST接口调用请求头请设置 "Content-Type","application/x-www-form-urlencoded; charset=utf-8"
     */
    public static String testRestful(String address,String api,String mainDataStr,String detailStr,String userId){
        //ECOLOGY返回的token
        String token= SYSTEM_CACHE.get("SERVER_TOKEN");
        if (StrUtil.isEmpty(token)){
            token = (String) testGetoken(address).get("token");
        }
        String spk = SYSTEM_CACHE.get("SERVER_PUBLIC_KEY");
        //封装请求头参数
        RSA rsa = new RSA(null,spk);
        //对用户信息进行加密传输,暂仅支持传输OA用户ID
        String encryptUserid = rsa.encryptBase64(userId,CharsetUtil.CHARSET_UTF_8,KeyType.PublicKey);
        System.out.println("人员id" + userId);
        Map<String,String> map = new HashMap();
        Map<String,Object> parasMap = new HashMap();
        //mainDataStr = "[{\"fieldName\":\"tdr\",\"fieldValue\":\"20980\"},{\"fieldName\":\"tdrbm\",\"fieldValue\":\"742\"},{\"fieldName\":\"sqrq\",\"fieldValue\":\"2023-07-17\"},{\"fieldName\":\"ddlx\",\"fieldValue\":\"test\"}]";
        //detailStr = "[{\"tableDBName\":\"formtable_main_279_dt1\",\"workflowRequestTableRecords\":[{\"recordOrder\":0,\"workflowRequestTableFields\":[{\"fieldName\":\"lyddbh\",\"fieldValue\":\"5555\"},{\"fieldName\":\"wlbh\",\"fieldValue\":\"66666\"},{\"fieldName\":\"wlmc\",\"fieldValue\":\"77777777\"},{\"fieldName\":\"pp\",\"fieldValue\":\"888888\"}]},{\"recordOrder\":0,\"workflowRequestTableFields\":[{\"fieldName\":\"lyddbh\",\"fieldValue\":\"5555\"},{\"fieldName\":\"wlbh\",\"fieldValue\":\"66666\"},{\"fieldName\":\"wlmc\",\"fieldValue\":\"77777777\"},{\"fieldName\":\"pp\",\"fieldValue\":\"888888\"}]}]}]";
        //创建参数集合
        parasMap.put("mainData",mainDataStr);
        parasMap.put("detailData",detailStr);
        parasMap.put("requestName","刀具柜采购收发货申请流程");
        parasMap.put("workflowId",279);
        parasMap.put("otherParams","{\"isnextflow\":1}");
        map.put("Content-Type","application/x-www-form-urlencoded; charset=utf-8");
        map.put("appid",APPID);
        map.put("token",token);
        map.put("userid",encryptUserid);
        String data = HttpRequest.post(address + api)
                .addHeaders(map)
                .form(parasMap)
                .execute().body();
        //System.out.println("testRestful()："+data);
        return data;
    }
}
